angular.module('BaseServer')

//我们把socket服务定义成了一个factory，这样它只会被实例化一次，而且在整个应用的作用域范围内就成为了一个单例
.factory('BaseSocket', ['BaseService', function(BaseService) {
	return {
		connect: function(resultHandel){
			BaseService.getSysConstValue('SocketPort', function(socketPort){
			if(socketPort != null && socketPort != ''){
				var socket = {
	            	Socket: io.connect('http://' + document.location.hostname + ":" + socketPort),
	            	//监听接收消息
					on: function(eventName, callback){
						if(this.Socket) {
							this.Socket.on(eventName, function(data){
								callback(data);
							});
						}
					},
					//发送消息
					emit: function(eventName, data) {
						if (this.Socket) {
							this.Socket.emit(eventName, data);
						}
					},
					//发给自己
					toSelf: function(eventName, sendData) {
				      if (this.Socket) {
				      	var data = {
				      		EventName: eventName,
				      		Data: sendData
				      	};
				        this.Socket.emit("toSelf", data);
				      }
			    	},
			    	//发给除自己的所有人
			    	toOther: function(eventName, sendData) {
				      if (this.Socket) {
				      	var data = {
				      		EventName: eventName,
				      		Data: sendData
				      	};
				        this.Socket.emit("toOther", data);
				      }
			    	},
			    	//发给所有人，包括自己
			    	toAll: function(eventName, sendData) {
				      if (this.Socket) {
				      	var data = {
				      		EventName: eventName,
				      		Data: sendData
				      	};
				        this.Socket.emit("toAll", data);
				      }
			    	},
			    	//加入房间
			    	joinRoom: function(roomName) {
				      if (this.Socket) {
				      	var data = {
				      		RoomName: roomName
				      	};
				        this.Socket.emit("joinRoom", data);
				      }
			    	},
			    	//离开房间
			    	leaveRoom: function(roomName) {
				      if (this.Socket) {
				      	var data = {
				      		RoomName: roomName
				      	};
				        this.Socket.emit("leaveRoom", data);
				      }
			    	},
			    	//向指定房间发送消息，不包括自己
			    	toRoom: function(roomName, eventName, sendData) {
				      if (this.Socket) {
				      	var data = {
				      		RoomName: roomName,
				      		EventName: eventName,
				      		Data: sendData
				      	};
				        this.Socket.emit("toRoom", data);
				      }
			    	},
			    	//向指定房间发送消息，包括自己
			    	inRoom: function(roomName, eventName, sendData) {
				      if (this.Socket) {
				      	var data = {
				      		RoomName: roomName,
				      		EventName: eventName,
				      		Data: sendData
				      	};
				        this.Socket.emit("inRoom", data);
				      }
			    	},
			    	disconnect: function(){
			    		this.Socket.emit("clientDisconnect");
			    	}
			    }

			    if(socket.Socket) {
		    		resultHandel(socket);
		    	}
		    	else{
		    		var newErr = {
	                    leval: 5,
	                    title: '获取系统参数SocketPort失败',
	                    message: '请检查系统参数配置是否正确'
	                }
	                BaseFunction.errFun(newErr);
	            	resultHandel(null);
		    	}
            }
            else{
            	var newErr = {
                    leval: 5,
                    title: '获取系统参数SocketPort失败',
                    message: '请检查系统参数配置是否正确'
                }
                BaseFunction.errFun(newErr);
            	resultHandel(null);
            }
        });
		}		
	};
}]);